package com.bw.service.impl;

import com.bw.domain.Role;
import com.bw.exception.MyRuntimeException;
import com.bw.mapper.RoleMapper;
import com.bw.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author :Beyond🎸🎸
 * @date : 2025-03-03 20:19:01
 * @description Rock and Roll
 **/
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Transactional(rollbackFor = com.bw.exception.MyRuntimeException.class)
    @Override
    public Boolean add(Role role, String menuIds) {

        try {
            roleMapper.add(role);//保存主表
            if (!StringUtils.isEmpty(menuIds)) {
                String[] split = menuIds.split(",");
                for (int i = 0; i < split.length; i++) {
                    roleMapper.addRoleMenu(role.getId(), split[i]);//保存从表
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            new  MyRuntimeException("添加角色失败");
        }
        return false;
    }
}
